home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / amiga / bmake15.lzh / parsing.c < prev    next >
C/C++ Source or Header  |  1991-09-09  |  2KB  |  147 lines

  1. /*    Parsing.c
  2.  *    (c) Copyright 1991 by Ben Eng, All Rights Reserved
  3.  *
  4.  */
  5.  
  6. #include <string.h>
  7. #include <ctype.h>
  8.  
  9. /*    Parse the first word from the source string into the dest string
  10.  *
  11.  *    return: pointer to next whitespace past parsed argument
  12.  */
  13.  
  14. char *
  15. parse_strtok( char *dest, char *source, int len, int (*isdelim)( int ))
  16. {
  17.     register char *s, *d;
  18.     int i = 0;
  19.  
  20.     s = source;
  21.     d=dest;
  22.     while( i < len && *s ) {
  23.         if( (*isdelim)( *s ))
  24.             break;
  25.         *d++ = *s++;
  26.         i++;
  27.     }
  28.     *d=(char)0;
  29.     return s;
  30. }
  31.  
  32.  
  33. static int
  34. iswhite( int ch )
  35. {
  36.     return( isspace( ch ));    /* macro */
  37. }
  38.  
  39. /*    Parse the first word from the source string into the dest string
  40.  *
  41.  *    return: pointer to next whitespace past parsed argument
  42.  */
  43. char *
  44. parse_str( char *dest, char *source, int len )
  45. {
  46.     while( isspace( *source )) source++;    /* skip whitespaces */
  47.     return( parse_strtok( dest, source, len - 1, iswhite ));
  48. }
  49.  
  50. /*    Counts the number of words in the string */
  51. int
  52. count_args( unsigned char *string )
  53. {
  54.     int count, sflag, prev;
  55.     register unsigned char *a = string;
  56.  
  57.     prev = 1;
  58.     count = 0;
  59.     for( ; *a; a++ ) {
  60.         sflag = isspace( *a );
  61.         if( !sflag && prev )
  62.             count++;
  63.         prev = sflag;
  64.     }
  65.     return count;
  66. }
  67.  
  68. /*    finds the position of a particular word in a string */
  69. /*    the first word is numbered 1 */
  70. /*    find_word( string, count_args( string )) == last_word */
  71. char *
  72. find_word( char *string, int word )
  73. {
  74.     int count, sflag, prev;
  75.     register char *a = string;
  76.  
  77.     prev = 1;
  78.     count = 0;
  79.     for( ; *a; a++ ) {
  80.         sflag = isspace( *a );
  81.         if( !sflag && prev )
  82.             if( ++count == word )
  83.                 return( a );
  84.         prev = sflag;
  85.     }
  86.     return( NULL ); /* not found */
  87. }
  88.  
  89. void
  90. strip_trailspace( char *line )
  91. {
  92.     char *cptr;
  93.  
  94.     cptr = line + strlen( line ) - 1;
  95.  
  96.     while( cptr >= line && isspace( *cptr )) {
  97.         *cptr-- = (char)0;
  98.     }
  99. }
  100.  
  101. int
  102. isnotsuf( int ch )
  103. {
  104.     if( !ch || isspace( ch ) || ch == '.' )
  105.         return( 1 );
  106.     return( 0 );
  107. }
  108.  
  109. int
  110. isemptyline( char *line )
  111. {
  112.     while( *line ) {
  113.         if( !isspace( *line ))
  114.             return( 0 );
  115.         line++;
  116.     }
  117.     return( 1 );
  118. }
  119.  
  120. /*    locate the next instance of a token that is not escaped by a backslash
  121.  */
  122. char *
  123. find_token( char *line, int tok )
  124. {
  125.     char *delim = strchr( line, tok );
  126.     while( delim && *(delim-1) == '\\' ) {
  127.         delim = strchr( delim + 1, tok );
  128.     }
  129.     return( delim );
  130. }
  131.  
  132. void
  133. shift_string_left( char *string, int shift )
  134. {
  135.     register char *d, *s;
  136.     char *end = string + strlen( string );
  137.  
  138.     if( shift > 0 ) {
  139.         d = string;
  140.         s = string + shift;
  141.         while( s <= end ) {
  142.             *d++ = *s++;
  143.         }
  144.     }
  145. }
  146.  
  147.